<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/register.css">
</head>
<body>
<div class="container-login">
    <div class="container-pic">
    </div>
    <div class="login-dialog">
        <h3>新用户注册</h3>
        <form id="registerForm">
            <div class="row">
                <span>账户</span>
                <input type="text" name="username" id="username" class="form-control" required>
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password" class="form-control" required>
            </div>
            <div class="row">
                <span>确认密码</span>
                <input type="password" name="confirmPassword" id="confirmPassword" class="form-control" required>
            </div>
            <div class="row" style="margin-top: 20px;">
                <div class="col-md-6">
                    <button type="button" class="btn btn-success btn-lg btn-block" onclick="submitRegister()">立即注册</button>
                </div>
                <div class="col-md-6">
                    <button type="button" class="btn btn-secondary btn-lg btn-block" onclick="history.back()">返回登录</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="js/jquery.min.js"></script>
<script>
    function submitRegister() {
        // 基础验证
        if ($("#password").val() !== $("#confirmPassword").val()) {
            alert("两次密码输入不一致！");
            return;
        }

        $.ajax({
            url: "/user/register",
            type: "post",
            data: {
                "userName": $("#username").val(),
                "password": $("#password").val()
            },
            success: function(result) { //返回空字符串表示注册成功，反之注册不成功
                if(result.status=="SUCCESS") {
                    alert("注册成功！");
                    location.href = "login_test.html";
                } else {
                    alert( "注册失败，请重试");
                }
            }
        });
    }
</script>
</body>
</html>